%load alt_calib/eq_mit_mass_shock.mat
clear
%load ../parameter_sandbox_wage_rule/eq_mit_mass.mat 
load ../../free_entry/eq_mit_entry.mat
%load ../prod/calibration_v3/sandbox/eq_mit_mass

set(0,'defaultaxesfontname','cambria math') % beautify the axes a bit
set(0,'defaultTextFontName', 'cambria math')

Ns = 1 - [7.13]/100;

adj = sum(eq_mit.mu,1);
addpath ../
scx = pick_scale(adj, Ns);

T = 15;

figure(47)
 
 Labor       =  eq_mit.l;
 total_labor =  sum(eq_mit.mu.*Labor);

 % 
 costs = eq_mit.l;
 prods = repmat(exp(glob.sf(:,2)), 1, options.T);

 wages = repmat(eq_mit.W', glob.Nsf, 1);
 mkps  = eq_mit.p./(wages./prods);
 mu = mkps;
% 
 tc    = sum(wages.*costs.*eq_mit.mu);
 num    = sum(wages.*costs.*eq_mit.mu.*mkps);
% 
 mu_cw = num./tc;

Ys    = repmat(eq_mit.C',glob.Nsf,1);
Z     = sum(eq_mit.y./Ys./prods.*eq_mit.mu);
Z     = Z.^(-1);


%% plot real output, labor demand, and nominal gdp
close all

Y = log_linear_scale(eq_mit.C, scx);

W = log_linear_scale(eq_mit.W, scx);

L = plot(log_linear_scale(total_labor, scx)/total_labor(end), 'LineWidth', 4);


ngdp = sum(eq_mit.mu.*eq_mit.p.*eq_mit.y);

labor_bill = eq_mit.W'.*sum(eq_mit.l.*eq_mit.mu);
labor_share = labor_bill./ngdp;

mu = log_linear_scale(1./labor_share, scx);

tfp = Z;%eq_mit.C'./total_labor;
A = log_linear_scale(tfp, scx)/tfp(end);

%% Counterfactuals

bl = three_equation_model(A, mu, .5);
fixed_mu = three_equation_model(A, mu(end), .5);
fixed_tfp = three_equation_model(A(end), mu, .5);

% subplot(2,1,1)
% plot(bl.Y, 'LineWidth', 4)
% hold on
% plot(fixed_mu.Y, '--', 'LineWidth', 4)
% plot(fixed_tfp.Y, '-.', 'LineWidth', 4)
% legend('Baseline', 'TFP effect', 'Markup effect', 'Location', 'SouthEast')
% xlim([1 T])
% title('Y')
% 
% subplot(2,1,2)


plot( bl.L, 'LineWidth', 4)
hold on
plot(fixed_mu.L, '--', 'LineWidth', 4)
plot(fixed_tfp.L, '-.', 'LineWidth', 4)
plot([0 15], [1 1], 'k--')


xlim([0 T-1])
ylim([.97 1.005])
title('L')
legend('Total effect', 'TFP effect', 'Markup effect', 'Location', 'SouthEast')

% subplot(2,2,3)
% plot(bl.W, 'LineWidth', 4)
% hold on
% plot(fixed_mu.W, '--', 'LineWidth', 4)
% plot(fixed_tfp.W, '-.', 'LineWidth', 4)
% xlim([1 T])
% title('W')
% 
% subplot(2,2,4)
% plot(A/A(end), 'LineWidth', 4)
% hold on
% plot(mu/mu(end), '--', 'LineWidth', 4)
% legend('TFP','Markup')
% xlim([1 T])

set(gcf,'units','points','position',[10,10,500,250])
set(findall(gcf,'-property','FontSize'),'FontSize',16)



print('-dpng', 'entry_shock_decomp.png')